package CI.aufgabe2.cip.mynodes;

import CI.aufgabe2.cip.SymbolTable;
import CI.aufgabe2.cip.base.AbstractDescr;
import CI.aufgabe2.cip.descriptor.IntConstDescr;

public class MyConstDeclarationNode extends MyAbstractNode {

	private static final long serialVersionUID = 1L;
	
	private final MyIdNode ident;
	private final MyAbstractNode expression;
	
	public MyConstDeclarationNode(MyIdNode ident, MyAbstractNode expression) {
		this.ident=ident;
		this.expression=expression;
	}

	@Override
	public void print() {
		trace("ConstDeclarationNode");
		if(ident != null)
		ident.print();
		
		if(expression != null)
		expression.print();
		
		unindent();

	}
	
	
	public AbstractDescr compile(SymbolTable sym) {
		IntConstDescr value = new IntConstDescr(); 
		value.setSize(1);
//		if(expression instanceof MyBinNode){
//			value = value.setIntVal(((MyBinNode)expression).g);
//		}
		if(expression instanceof MyIntNode){
			value.setIntVal(((MyIntNode)expression).getIntVal());
			
		}
		sym.declare(ident.toString(), value);
		write("GETSP");
		write("PUSHI, "+value.getSize());
		write("ADD");
		write("SETSP");
		write("PUSHI, "+value.getIntVal());
		write("PUSHI, "+sym.getAdr(ident.toString()));
		write("ASSIGN, 1");
		return value;
	}

}
